﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using LGCourt.Logic.BLL;
using LGCourt.Logic.NBL.DBEntities;
using KMFrame.Permission.Util;

namespace lgweb.user
{
    public partial class appcation : System.Web.UI.Page
    {
        private int fileMaxSize = 1024 * 1024 * 10;
        private string _AppContent = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!SessionBox.CheckUserSession())
            {
                Response.Redirect("/user/login.aspx");
            }
        }

        //获取参数值
        private void getApplicationParam()
        {
            this._AppContent = this.AppContent.Value;

        }

        private void createrInfo()
        {
            PreCassCollection param = new PreCassCollection();

            int _userID = SessionBox.GetUserSession().UserID;

            pPreCaseInfo pCinfo = new pPreCaseInfo();
            pCinfo.PreCaseType = 1;
            pCinfo.UserID = _userID;
            pCinfo.Lawsuit = this.txtLawsuit.Value; //诉讼请求
            pCinfo.Reason = this.txtReason.Value;  //理由与事实

            param.PreCaseInfo = pCinfo;

            List<pPreParties> mPartiesList = new List<pPreParties>();
            string[] aApps = this._AppContent.Split('$');//切分为原被告
            for (int a = 0; a < aApps.Length; a++)
            {
                string[] alis = aApps[a].Split('~');//切分有多少个原被告
                for (int l = 1; l < alis.Length; l++)
                {
                    string[] aentity = alis[l].Split('&');//切分出每一个元素
                    pPreParties tmpPP = new pPreParties();
                    tmpPP.PartiesType = short.Parse(aentity[0]);
                    tmpPP.PartiesClass = short.Parse(aentity[1]);
                    tmpPP.PartiesName = aentity[2];

                    switch (aentity[1])
                    {
                        case "1":
                            tmpPP.IdentityCard = aentity[3];
                            tmpPP.Birth = aentity[4];
                            tmpPP.Nationaly = aentity[5];
                            tmpPP.National = aentity[6];
                            tmpPP.Political = aentity[7];
                            tmpPP.Degree = aentity[8];
                            tmpPP.Phone1 = aentity[9];
                            tmpPP.PostalCode = aentity[10];
                            tmpPP.Domicile = aentity[11];
                            tmpPP.Address = aentity[12];
                            break;

                        case "2":
                            tmpPP.LegalPerson = aentity[3];
                            tmpPP.Phone1 = aentity[4];
                            tmpPP.PostalCode = aentity[5];
                            tmpPP.Domicile = aentity[6];
                            tmpPP.Address = aentity[7];
                            break;
                        case "3":
                            tmpPP.LegalPerson = aentity[3];
                            tmpPP.Phone1 = aentity[4];
                            tmpPP.PostalCode = aentity[5];
                            tmpPP.Domicile = aentity[6];
                            tmpPP.Address = aentity[7];
                            break;
                    }
                    mPartiesList.Add(tmpPP);
                }
            }

            List<pPreCaseFile> mFileList = new List<pPreCaseFile>();
            //string[] mfiles = this._AppContent.Split('~');//切分为每个文件
            //for (int a = 0; a < mfiles.Length; a++)
            //{
            try
            {
                doUpload();
                pPreCaseFile casefile = new pPreCaseFile();
                casefile.FileName = this.FileUpload1.FileName;
                string tempDir = "~//file//yulian//";
                string urlPath = tempDir + this.FileUpload1.FileName;
                byte[] ib = new Byte[60000];
                FileStream fs = new FileStream(Server.MapPath(urlPath), FileMode.Open, FileAccess.Read);
                fs.Read(ib, 0, 60000);

                casefile.FileContent = ib;
                mFileList.Add(casefile);
            }
            catch
            {
            }
            //}
            //当事人列表
            param.PrePartiesList = mPartiesList;


            //文件列表
            param.PreCaseFile = mFileList;

            PreCase bll = new PreCase();
            string result = (string)bll.CreatePreCase(param);

            if (result.Length > 0)
            {
                Response.Redirect("/user/casedetail.aspx?num=" + result);
            }
            else
            {
                this.divMessage.Visible = true;
            }
        }

        protected void doUpload()
        {
            try
            {

                if (this.FileUpload1.HasFile)
                {
                    if (this.FileUpload1.PostedFile.ContentLength > this.fileMaxSize)//file.FileSize > this.fileMaxSize
                    {
                        throw new Exception("文件超过允许的大小！");
                    }
                    string tempDir = "~//file//yulian//";
                    string urlPath = tempDir + this.FileUpload1.FileName;

                    if (!Directory.Exists(Server.MapPath(tempDir)))
                    {
                        Directory.CreateDirectory(Server.MapPath(tempDir));
                    }
                    //上传文件并指定上传目录的路径   
                    this.FileUpload1.PostedFile.SaveAs(Server.MapPath(urlPath));

                }

            }
            catch (Exception ex)
            {
                //WriteJs("objUploadFile.uploaderror('" + ex.Message + "');");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            getApplicationParam();
            createrInfo();
        }
    }
}